package com.ruoyi.pay.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.pay.Exception.ChannelException;
import com.ruoyi.pay.channel.IPaymentService;
import com.ruoyi.pay.consts.ApiCodeEnum;
import com.ruoyi.pay.consts.CS;
import com.ruoyi.pay.domain.*;
import com.ruoyi.pay.mapper.PayOrderMapper;
import com.ruoyi.pay.model.BaseModel;
import com.ruoyi.pay.model.params.NormalMchParams;
import com.ruoyi.pay.msg.ChannelRetMsg;
import com.ruoyi.pay.service.*;
import com.ruoyi.pay.utils.*;
import com.ruoyi.pay.vo.*;
import com.ruoyi.pay.mapper.PayWayMapper;
import com.ruoyi.pay.payway.payOrder.UnifiedOrderRQ;
import com.ruoyi.pay.payway.payOrder.UnifiedOrderRS;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.rabbitMq.model.PayOrderReissueMQ;
import com.ruoyi.rabbitMq.send.IMQSender;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/*
 * 统一下单 controller
 *
 */
@Slf4j
@RestController
public class UnifiedOrderController extends BaseController {

    @Autowired
    private ValidateService validateService;

    @Resource
    private PayWayMapper payWayService;


    @Autowired
    private PayOrderService payOrderService;

    @Autowired
    private ConfigContextQueryService configContextQueryService;

    /**
     * 统一下单接口
     **/
    @PostMapping("/api/pay/unifiedOrder")
    public ApiRes unifiedOrder() {
        try {
            //HttpServletRequest获取参数，重新加签，对比传入的加签参数
            UnifiedOrderRQ rq = payOrderService.getRQByWithMchSign(UnifiedOrderRQ.class);

            //构建支付接口:实现类 AliBarOrderRQ
            UnifiedOrderRQ bizRQ  = payOrderService.buildBizRQ(rq);

            //FiXME 生成订单: 调起支付接口返回,比如生成二维码
            ApiRes apiRes = payOrderService.unifiedOrder(bizRQ.getWayCode(), bizRQ);
            if (apiRes.getData() == null) {
                return apiRes;
            }

            UnifiedOrderRS bizRes = (UnifiedOrderRS) apiRes.getData();

            //聚合接口，返回的参数
            UnifiedOrderRS res =  new UnifiedOrderRS();
            BeanUtils.copyProperties(bizRes, res);

            //只有 订单生成（QR_CASHIER） || 支付中 || 支付成功返回该数据
            if (bizRes.getOrderState() != null && (bizRes.getOrderState() == PayOrder.STATE_INIT || bizRes.getOrderState() == PayOrder.STATE_ING || bizRes.getOrderState() == PayOrder.STATE_SUCCESS)) {
                res.setPayDataType(bizRes.buildPayDataType());
                res.setPayData(bizRes.buildPayData());
            }
             String appSecret = configContextQueryService.queryMchApp(rq.getMchNo(), rq.getAppId()).getAppSecret();
             ApiRes apiRes1 = ApiRes.okWithSign(res, appSecret);
            return apiRes1;
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage(), e);
            return ApiRes.fail(ApiCodeEnum.SYSTEM_ERROR, e.getMessage());
        }
    }






}
